package com.example.demo.service.Impl;

import com.example.demo.entity.*;
import com.example.demo.mapper.TaskFamilyMapper;
import com.example.demo.mapper.TaskMapper;
import com.example.demo.mapper.TaskMemberMapper;
import com.example.demo.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    TaskMapper taskMapper;
    @Autowired
    TaskFamilyMapper taskFamilyMapper;
    @Autowired
    TaskMemberMapper taskMemberMapper;

    //新建任务
    @Override
    public int addTask(Task task){
        return taskMapper.insert(task);
    }

    //增加任务成员
    public int addTaskUser(TaskMember taskMember){
        return taskMemberMapper.insert(taskMember);
    }

    //任务具体信息返回
    @Override
    public List<Task> getTask(Integer userId){
        List<Integer> taskIds = new ArrayList<>();
        TaskMemberExample taskMemberExample = new TaskMemberExample();
        TaskMemberExample.Criteria criteria = taskMemberExample.createCriteria();
        criteria.andTaskMemUserIdEqualTo(userId);
        List<TaskMember> userIds =taskMemberMapper.selectByExample(taskMemberExample);
        System.out.println("userID："+userIds);
        if(userIds.isEmpty())
            return Collections.EMPTY_LIST;

        for (TaskMember taskMember:userIds){
            taskIds.add(taskMember.getTaskMemTaskId());
        }
        System.out.println("任务ID："+taskIds);

        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria1 = taskExample.createCriteria();
        criteria1.andTaskIdIn(taskIds);
        List<Task> tasks = taskMapper.selectByExample(taskExample);
        System.out.println(tasks);
        return tasks;

    }

    //获取子任务
    @Override
    public List<Task> getChildTask(Integer taskId){
        List<Integer> taskIds = new ArrayList<>();
        TaskFamilyExample taskFamilyExample= new TaskFamilyExample();
        TaskFamilyExample.Criteria criteria = taskFamilyExample.createCriteria();
        criteria.andTaskFatherIdEqualTo(taskId);
        List<TaskFamily> taskIDS = taskFamilyMapper.selectByExample(taskFamilyExample);//根据任务ID获取子任务列表
        System.out.println(taskIDS);
        if (taskIDS.isEmpty())
            return Collections.EMPTY_LIST;

        for (TaskFamily taskFamily: taskIDS){
            taskIds.add(taskFamily.getTaskChildId()); //获取该任务成员的taskId
        }
        System.out.println(taskIds);

        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria1 = taskExample.createCriteria();
        criteria1.andTaskIdIn(taskIds);
        List<Task> tasks = taskMapper.selectByExample(taskExample);
        System.out.println(tasks);
        return tasks;
    }

    //获取父任务
    @Override
    public List<Task> getFatherTask(Integer taskId) {
        List<Integer> taskIds = new ArrayList<>();
        TaskFamilyExample taskFamilyExample= new TaskFamilyExample();
        TaskFamilyExample.Criteria criteria = taskFamilyExample.createCriteria();
        criteria.andTaskChildIdEqualTo(taskId);
        List<TaskFamily> taskIDS = taskFamilyMapper.selectByExample(taskFamilyExample);
        System.out.println(taskIDS);
        if (taskIDS.isEmpty())
            return Collections.EMPTY_LIST;

        for (TaskFamily taskFamily: taskIDS){
            taskIds.add(taskFamily.getTaskFatherId()); //获取该任务成员的taskId
        }
        System.out.println(taskIds);

        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria1 = taskExample.createCriteria();
        criteria1.andTaskIdIn(taskIds);
        List<Task> tasks = taskMapper.selectByExample(taskExample);
        System.out.println(tasks);
        return tasks;
    }

    //修改任务
    @Override
    public int changeTask(Integer taskId, String taskName, String taskContent, Date taskOpenTime, Date taskCompleteTime, Integer taskFunctionId){
        Task task = new Task();
        task.setTaskName(taskName);
        task.setTaskContent(taskContent);
        task.setTaskOpenTime(taskOpenTime);
        task.setTaskCompleteTime(taskCompleteTime);
        task.setTaskFunctionId(taskFunctionId);
        return taskMapper.updateByPrimaryKeySelective(task);
    }

    @Override
    public List<Task> getProTaskList(Integer proId) {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andTaskProEqualTo(proId);
        List<Task> taskList = taskMapper.selectByExample(taskExample);
        return taskList;
    }

    @Override
    public long proTaskNum(Integer proId) {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andTaskProEqualTo(proId);
        return taskMapper.countByExample(taskExample);
    }
}
